package com.easylive.web.task;

//TODO 改为mq消息队列

import com.easylive.component.EsSearchComponent;
import com.easylive.component.RedisComponent;
import com.easylive.entity.constants.Constants;
import com.easylive.entity.dto.VideoPlayInfoDto;
import com.easylive.entity.enums.EsCountFieldEnum;
import com.easylive.entity.po.VideoInfoFilePost;
import com.easylive.entity.po.VideoInfoPost;
import com.easylive.redis.RedisUtils;
import com.easylive.service.VideoInfoPostService;
import com.easylive.service.VideoInfoService;
import com.easylive.service.impl.VideoInfoServiceImpl;
import com.easylive.utils.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
@Slf4j
public class ExecuteQueueTask {
    @Resource
    private RedisComponent redisComponent;

    private ExecutorService executorService = Executors.newFixedThreadPool(Constants.Length_2);

    @Resource
    private VideoInfoPostService videoInfoPostService;
    @Autowired
    private VideoInfoService videoInfoService;
    @Autowired
    private EsSearchComponent esSearchComponent;


    @PostConstruct
    public void consumTransferFileQueue(){
        log.info("开始消费待转码文件列表---------------------------------------------------");
        executorService.execute(() -> {
            while (true){
                //TODO 获取待转码文件列表
                try{
                    VideoInfoFilePost videoInfoFile = redisComponent.getFileFromTransferQueue();
                    if(videoInfoFile == null){
                        Thread.sleep(20000);
                        continue;
                    }
                    videoInfoPostService.transferVideoFile(videoInfoFile);
                }catch (Exception e){
                        log.error("获取待转码文件列表失败！",e);
                }
            }
        });
    }

    @PostConstruct
    public void consumPlayCountFileQueue(){
        log.info("开始消费视频播放次数列表---------------------------------------------------");
        executorService.execute(() -> {
            while (true){
                //TODO 获取待转码文件列表
                try{
                    VideoPlayInfoDto videoPlayInfoDto =  redisComponent.getVideoPlayInfo();
                    if(videoPlayInfoDto == null){
                        Thread.sleep(20000);
                        continue;
                    }
                    videoInfoService.addVideoCount(videoPlayInfoDto.getVideoId());
                    if(!StringTools.isEmpty(videoPlayInfoDto.getUserId())){
                        //TODO记录历史
                    }
                    //TODO按天数记录视频播放
                    redisComponent.recordVideoPlayCount(videoPlayInfoDto.getVideoId());
                    //更新es
                    esSearchComponent.updateDocCount(videoPlayInfoDto.getVideoId(), EsCountFieldEnum.VIDEO_PLAY.getFieldName(),1);
                }catch (Exception e){
                    log.error("获取视频播放列表失败！",e);
                }
            }
        });
    }


}
